package cn.picclife.mwx.app.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.Inet4Address;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties;

public class PropertiesUtil {

	private static final String CONFIG_PATH = "properties/dev/";
	private static final String FILE_SUFFIX = ".properties";
	private static final String FILE_SUFFIX_INI = ".ini";
	private static final Map<String, Properties> propMap = new HashMap<String, Properties>();
	private static Log log = LogFactory.getLog(PropertiesUtil.class);

	private PropertiesUtil() {
	}

	/**
	 * 读取src目录下的配置文件
	 * 
	 * @param file
	 * @return
	 * @throws IOException
	 */
	/*public static Properties getPropertiesBySrc(String fileName) {
		InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(CONFIG_PATH + fileName);
		Properties prop = new Properties();
		try {
			String key = fileName.substring(0, fileName.indexOf("."));
			if (propMap.get(key) == null) {
				prop.load(in);
			} else {
				prop = propMap.get(key);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (in != null)
					in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return prop;
	}*/

	/**
	 * 读取src目录下的配置文件
	 */
	public static  Map<String, Properties> loadPropFile(String fileName) {
		String key = fileName;
		if (propMap.get(key) != null) {
			return propMap;
		}
		fileName = fileName + FILE_SUFFIX;
		InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(CONFIG_PATH + fileName);
		Properties prop = new Properties();
		try {
			prop.load(in);
			propMap.put(key, prop);
		} catch (IOException e) {
//			e.printStackTrace();
//			抛出一个运行时异常
			log.error(e);
		} finally {
			try {
				if (in != null)
					in.close();
			} catch (IOException e) {
				log.error(e);
			}
		}
		return propMap;
	}


	public static void loadPropFileIni(String fileName) {
		String key = fileName;
		if (propMap.get(key) != null) {
			return;
		}
		fileName = fileName + FILE_SUFFIX_INI;
		InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(CONFIG_PATH + fileName);
		Properties prop = new Properties();
		try {
			prop.load(in);
			propMap.put(key, prop);
		} catch (IOException e) {
			log.error(e);
		} finally {
			try {
				if (in != null)
					in.close();
			} catch (IOException e) {
				log.error(e);
			}
		}
	}

	/**
	 * 读取配置文件的属性值
	 * 不再推荐使用，使用了getOSPropBySrc方法替代
	 * @param fileName
	 * @return
	 */
	@Deprecated
	public static String getPropertiesBySrc(String fileName, String propKey) {
		String key = fileName;
		if (propMap.get(key) == null) {
			loadPropFile(fileName);
		}
		try {
			return new String(String.valueOf(propMap.get(fileName).get(propKey)).getBytes("ISO-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			log.error(e);
		}
		;
		return null;
	}
	/**
	 * 方法功能同getPropertiesBySrc，只是添加了根据系统的不同，
	 * 获取不同系统下配置的key
	 * 注意，使用之前请先看明白
	 * @param fileName
	 * @param propKey
	 * @return
	 */
	public static String getOSPropBySrc(String fileName, String propKey) {
		String key = fileName;
		if (propMap.get(key) == null) {
			loadPropFile(fileName);
		}
		/*
		 * 判断是window还是linux系统，
		 * 当是linux系统的时候,添加后缀"linux"
		 */
//		String osName = System.getProperty("os.name");
//		if (!osName.contains("Windows")) {
//			propKey+="_linux";
//		}
		try {
			return new String(String.valueOf(propMap.get(fileName).get(propKey)).getBytes("ISO-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			log.error(e);
		}
		;
		return null;
	}

	public static String getPropertiesBySrcIni(String fileName, String propKey) {
		String key = fileName;
		if (propMap.get(key) == null) {
			loadPropFileIni(fileName);
		}
		try {
			return new String(String.valueOf(propMap.get(fileName).get(propKey)).getBytes("ISO-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			log.error(e);
		}
		return null;
	}

	// 获取服务器IP
	public static final String printIp() {
		Inet4Address inet4Address = null;
		try {
			for (Enumeration e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
				NetworkInterface item = (NetworkInterface) e.nextElement();
				// 非虚拟非回路并开启状态
				if ((!item.isVirtual()) && item.isUp() && (!item.isLoopback())) {
					for (InterfaceAddress address : item.getInterfaceAddresses()) {
						if (address.getAddress() instanceof Inet4Address) {
							inet4Address = (Inet4Address) address.getAddress();
						}
					}
				}
			}
		} catch (IOException ex) {
		}
		return inet4Address.getHostAddress();
	}

	// 获取服务器IP
	public static void main(String[] args) {
		Map<String, Properties> propMap  =PropertiesUtil.loadPropFile("upload");
		Hashtable<Object,Object> table=propMap.get("upload");
		String MyPacketUrl=table.get("url").toString();
		//System.out.println(MyPacketUrl);
	}
	
}

